package com.fowo.api.flow.config;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.datainterface.constants.DataTypeKind;
import com.fowo.api.datainterface.entity.DatainterfaceAuthority;
import com.fowo.api.datainterface.service.impl.DatainterfaceAuthorityServiceImpl;
import com.fowo.api.datainterface.util.Common;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 民生配置
 */
@Component
@Data
public class DingDingConfig implements InitializingBean {

    /**
     * 钉钉发送消息的接口路径
     */
    public static String SEND_MESSAGE;
    /**
     * 钉钉获取TOKEN的接口路径
     */
    public static String TOKEN;


    //--------------------------------------------部门--------------------------------------

    /**
     * 创建部门
     */
    public static String CREATE_DEPARTMENT;

    /**
     * 更新部门
     */
    public static String UPDATE_DEPARTMENT;

    /**
     * 删除部门
     */
    public static String DELETE_DEPARTMENT;

    /**
     * 获取部门列表
     */
    public static String GET_DEPARTMENT_LIST;

    /**
     * 获取单个部门信息
     */
    public static String GET_DEPARTMENT_INFO;


    //-------------------------------------------用户-----------------------------------------------------

    /**
     * 创建用户
     */
    public static String CREATE_USER;

    /**
     * 更新用户
     */
    public static String UPDATE_USER;

    /**
     * 删除用户
     */
    public static String DELETE_USER;

    /**
     * 获取用户列表(返回精简的员工信息列表)
     */
    public static String GET_USER_LIST;

    /**
     * 获取用户列表(返回详细的员工信息列表)
     */
    public static String GET_USER_DETAIL_LIST;

    /**
     * 获取单个成员信息
     */
    public static String GET_SINGLE_USER;


    public static String APPKEY;
    public static String APPSECRET;
    public static String APPURL;
    public static String H5URL;
    public static String AGENTID;
    public static Boolean isOpen=true;

    public void afterPropertiesSet() throws Exception {
        DatainterfaceAuthority datainterfaceAuthority = SpringUtil.getBean(DatainterfaceAuthorityServiceImpl.class).getOne(Wrappers.<DatainterfaceAuthority>lambdaQuery().eq(DatainterfaceAuthority::getAppCode, DataTypeKind.DINGDING.getValue()), false);
        Assert.isTrue(ObjectUtil.isNotEmpty(datainterfaceAuthority), "钉钉授权数据信息不存在，请检查配置!");
        APPKEY = SpringUtil.getBean(Common.class).getAuthoritySimpleFactory(datainterfaceAuthority, "appkey");
        APPSECRET = SpringUtil.getBean(Common.class).getAuthoritySimpleFactory(datainterfaceAuthority, "appsecret");
        APPURL = datainterfaceAuthority.getRootPath();
        H5URL = SpringUtil.getBean(Common.class).getAuthoritySimpleFactory(datainterfaceAuthority, "h5Url");
        AGENTID = SpringUtil.getBean(Common.class).getAuthoritySimpleFactory(datainterfaceAuthority, "agentId");
        if(SpringUtil.getBean(Common.class).getAuthoritySimpleFactory(datainterfaceAuthority, "isOpen")!=null){
            isOpen = Boolean.valueOf(SpringUtil.getBean(Common.class).getAuthoritySimpleFactory(datainterfaceAuthority, "isOpen"));
        }
        SEND_MESSAGE = StrUtil.concat(true, APPURL, "/topapi/message/corpconversation/asyncsend_v2");
        TOKEN = StrUtil.concat(true, APPURL, "/gettoken");
        CREATE_DEPARTMENT = StrUtil.concat(true, APPURL, "/topapi/v2/department/create");
        UPDATE_DEPARTMENT = StrUtil.concat(true, APPURL, "/topapi/v2/department/update");
        DELETE_DEPARTMENT = StrUtil.concat(true, APPURL, "/topapi/v2/department/delete");
        GET_DEPARTMENT_LIST = StrUtil.concat(true, APPURL, "/topapi/v2/department/listsub");
        GET_DEPARTMENT_INFO = StrUtil.concat(true, APPURL, "/topapi/v2/department/get");
        CREATE_USER = StrUtil.concat(true, APPURL, "/topapi/v2/user/create");
        UPDATE_USER = StrUtil.concat(true, APPURL, "/topapi/v2/user/update");
        DELETE_USER = StrUtil.concat(true, APPURL, "/topapi/v2/user/delete");
        GET_USER_LIST = StrUtil.concat(true, APPURL, "/topapi/user/listsimple");
        GET_USER_DETAIL_LIST = StrUtil.concat(true, APPURL, "/topapi/v2/user/list");
        GET_SINGLE_USER = StrUtil.concat(true, APPURL, "/topapi/v2/user/get");
    }
}
